package com.erdos.core.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.erdos.common.service.BaseCRUDService;
import com.erdos.core.constant.ReferenceConstant;
import com.erdos.core.dao.AttributeDao;
import com.erdos.core.entity.Attribute;
import com.erdos.core.entity.RefTableDetail;
import com.erdos.core.service.AttributeService;
import com.erdos.core.service.ReferenceService;
import com.erdos.upc.service.impl.UserServiceImpl;

public class AttributeServiceImpl extends BaseCRUDService<Attribute> implements AttributeService{
	
	private static final Log logger = LogFactory.getLog(UserServiceImpl.class);

	private AttributeDao attributeDao;
	//操作MES_OBJECT_ATTRIBUTE表的DAO
	private ReferenceService referenceService;
	
	/**
	 * 查询对象、工厂下已有的Field主键集合
	 * @return
	 */
	public List<Long> findExistFieldSids(String object, String facilityID) {
		logger.debug("execute findExistFieldSids!");
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("facilityId", facilityID);
		paramMap.put("object", object);
		List<Long> resultList = attributeDao.findExistFieldSids(paramMap);
		logger.debug("execute over!"+resultList);
		return resultList;
	}

	/**
	 * 查询工厂列表，用于前端下拉框展示
	 * @return
	 */
	public Map<Long,String> findFacilityMap() {
		
		logger.debug("execute Map!");
		Map<Long,String> map = attributeDao.findFacilityMap();
		logger.debug("execute Map!"+map);
		return map;	
	}

	/**
	 * 查询对象、工厂下已有的分组列表
	 * @return
	 */
	public Map<String,String> findGroupMap(String object, String facilityID) {
		logger.debug("findGroupList start...");
		Map<String,Object> paramMap = new HashMap<String,Object>();
		//获取参数对象MAP
		paramMap.put("facilityId", facilityID);
		paramMap.put("object", object);
		Map<String,String> resultMap = attributeDao.findGroupMap(paramMap);
		//过滤分组信息中的空信息
		Map<String,String> correctMap = new HashMap<String,String>() ;
		
		Set<String> keyMapSet = resultMap.keySet();
		for(String key : keyMapSet){
			String groupValue = (String)resultMap.get(key);
			if(groupValue!= null && !groupValue.equals("") && key != null && !key.equals("")){
				correctMap.put(key, groupValue) ;
			}
		}
		logger.debug("findGroupList Execute! result:"+resultMap);
		return correctMap;
	}

	/**
	 * 查询对象列表，用于前端下拉框展示
	 * @return
	 */
	public Map<String,String> findObjectMap() {
		logger.debug("findObjectList start...");
		Map<String,String> resultMap= new HashMap<String,String>();
		
		List<RefTableDetail> refTableDetails=referenceService.findRefValueList(ReferenceConstant.REFERENCE_ATTRIBUTE_VALUE_MAP);
		for(RefTableDetail refTableDetail:refTableDetails){
			resultMap.put(refTableDetail.getKey1Value(), refTableDetail.getData2Value());
		}
//		resultMap.put("1", "One Object");
//		resultMap.put("2", "Tow Object");
		logger.debug("findObjectList Execute! result:"+resultMap);
		return resultMap;
	}


	/* (non-Javadoc)
	 * @see com.hp.core.service.AttributeService#isThereAnyValuesForTheAttribute(java.lang.String, java.lang.Long, java.lang.Long)
	 */
	public boolean isThereAnyValuesForTheAttribute(String object, Long feildSid) {
		
		Map<String,String> referenceMap=referenceService.findRefValueMap(ReferenceConstant.REFERENCE_ATTRIBUTE_VALUE_MAP);
		
		String tableName = referenceMap.get(object);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("tableName", tableName);
		params.put("object", object);
		params.put("feildSid", feildSid);
		int count = attributeDao.findAttributeCount(params);
		if(count >0 ){
			return true;
		}else{
			return false;
		}
	}
	


	/**
	 * @return the referenceService
	 */
	public ReferenceService getReferenceService() {
		return referenceService;
	}

	/**
	 * @param referenceService the referenceService to set
	 */
	public void setReferenceService(ReferenceService referenceService) {
		this.referenceService = referenceService;
	}

	/**
	 * @return the attributeDao
	 */
	public AttributeDao getAttributeDao() {
		return attributeDao;
	}

	/**
	 * @param attributeDao the attributeDao to set
	 */
	public void setAttributeDao(AttributeDao attributeDao) {
		this.attributeDao = attributeDao;
	}

}
